package com.hll.spring.basic;

import com.hll.spring.basic.pojo.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
  // 我觉得这个时候所有的bean都创建了
  private final ApplicationContext context =
      new ClassPathXmlApplicationContext("applicationContext.xml");

  @Test
  public void test1() {
    System.out.println("*********************test1***************");
    User user1 = (User) context.getBean("user1"); // 一般这种就够了
    System.out.println(user1);

    User user2 = context.getBean("user2", User.class); // 加上class不需要强转
    System.out.println(user2);

    // 与user1一样，别名
    User user3 = context.getBean("user3", User.class);
    System.out.println(user3);

    // 与user1一样，别名
    User user11 = context.getBean("user11", User.class);
    System.out.println(user11);

    User user4 = context.getBean("user4", User.class);
    System.out.println(user4);
  }

  @Test
  public void test2() {
    System.out.println("*********************test2***************");
    Student student = context.getBean("student", Student.class);
    System.out.println(student);
  }

  @Test
  public void test3() {
    System.out.println("*********************test3***************");
    OtherType student = context.getBean("otherType", OtherType.class);
    System.out.println(student);
  }

  @Test
  public void test4() {
    System.out.println("*********************test4***************");
    JDBCUtils jdbc = context.getBean("jdbc", JDBCUtils.class);
    System.out.println(jdbc);
  }

  @Test
  public void test5() {
    System.out.println("*********************test5***************");
    Student auto = context.getBean("stuAuto", Student.class);
    System.out.println(auto);
  }

  @Test
  public void test6() {
    System.out.println("*********************test6***************");
    ID id1 = context.getBean("singID2", ID.class);
    ID id2 = context.getBean("singID2", ID.class);
    System.out.println(id1 == id2); // false

    id1 = context.getBean("singID1", ID.class);
    id2 = context.getBean("singID1", ID.class);
    System.out.println(id1 == id2); // true
  }
}
